package com.controller;

import com.api.CommonResult;
import com.entity.BZpxs;
import com.entity.table.BJbxxTableDef;
import com.entity.table.BJbzpxsTableDef;
import com.entity.table.BZpxsTableDef;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.entity.BJbzpxs;
import com.service.BJbzpxsService;

import java.io.Serializable;
import java.util.List;

/**
 *  控制层。
 *
 * @author admin
 * @since 2024-06-24
 */
@Controller
@RequestMapping("/admin/bJbzpxs")
public class BJbzpxsController {

    @Autowired
    private BJbzpxsService bJbzpxsService;

    /**
     * 添加。
     *
     * @param bJbzpxs 
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
//    @PostMapping("save")
//    public boolean save(@RequestBody BJbzpxs bJbzpxs) {
//        return bJbzpxsService.save(bJbzpxs);
//    }

    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public boolean remove(@PathVariable Serializable id) {
        return bJbzpxsService.removeById(id);
    }

    /**
     * 根据主键更新。
     *
     * @param bJbzpxs 
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
//    @PutMapping("update")
//    public boolean update(@RequestBody BJbzpxs bJbzpxs) {
//        return bJbzpxsService.updateById(bJbzpxs);
//    }

    /**
     * 查询所有。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    public List<BJbzpxs> list() {
        return bJbzpxsService.list();
    }

    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @GetMapping("getInfo/{id}")
    public BJbzpxs getInfo(@PathVariable Serializable id) {
        return bJbzpxsService.getById(id);
    }

    /**
     * 分页查询。
     *s
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public Page<BJbzpxs> page(Page<BJbzpxs> page) {
        return bJbzpxsService.page(page);
    }

    @PostMapping("save")
    @ResponseBody
    public CommonResult save(BJbzpxs bJbzpxs) {
        try{
            boolean r = bJbzpxsService.save(bJbzpxs);
            return CommonResult.success(r);
        }catch (Exception e){
            e.printStackTrace();
            String msg = bJbzpxs.getTitle()+"已存在";
            return CommonResult.failed(msg);
        }

    }
    @PostMapping("update")
    @ResponseBody
    public CommonResult update(BJbzpxs bJbzpxs) {
        try{
            boolean r = bJbzpxsService.updateById(bJbzpxs);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = bJbzpxs.getTitle()+"已存在";
            return CommonResult.failed(msg);
        }
    }
    @GetMapping("LLList")
    public String bJzpxsList(ModelMap model, @RequestParam(value = "bJzpxsList",defaultValue = "") String bJzpxsList,
                           @RequestParam(value = "p", defaultValue = "1") int p,
                           @RequestParam(value = "pageSize", defaultValue = "10")int pageSize) {
        QueryWrapper queryWrapper = QueryWrapper.create().where(BJbzpxsTableDef.B_JBZPXS.TITLE.like(bJzpxsList));
        Page<BJbzpxs> page = new Page<>();
        page.setPageSize(pageSize);
        page.setPageNumber(p);
        Page<BJbzpxs> pageInfo = bJbzpxsService.page(page,queryWrapper);
        model.addAttribute("page", pageInfo);
        model.addAttribute("total", pageInfo.getTotalRow());
        return "/back/bJbzpxsTitle-list";
    }
    @GetMapping(value = "/addView")
    public  String addUserView(){ return "/back/bJbzpxsTitle-add";}

    @GetMapping(value = "updateView")
    public String
    updateView(int id,ModelMap model){
        BJbzpxs bJbzpxs = bJbzpxsService.getById(id);
        model.addAttribute("bJbzpxs",bJbzpxs);
        return "/back/bJbzpxsTitle-edit";
    }
    @PostMapping("del")
    @ResponseBody
    public CommonResult update(Integer id) {
        try {
            boolean r = bJbzpxsService.removeById(id);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }
    }
}



